<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="/sys/base :: headFragment(~{::title},~{::link},~{::style})"></head>
<body>
<div class="layui-form base-dialog">
    <form>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="password" name="pass" lay-verify="required|pass" autocomplete="off" placeholder="请输入新密码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="password" name="rePass" lay-verify="required|rePass" placeholder="再一次输入密码">
            </div>
        </div>
        <div class="layui-form-item base-finally">
            <button class="layui-btn" lay-submit lay-filter="update"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn btn-secondary base-close-dialog"><i class="fa fa-times-circle"></i> 取消</button>
        </div>
    </form>
</div>
</body>

<script th:replace="/sys/base :: scriptFragment"></script>
<!-- TODO RSA非对称加密 -->
<script th:src="@{/static/js/encrypt/jsencrypt.min.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['jquery', 'form'], function () {
        var $ = layui.jquery;
        var form = layui.form;

        /**
         * 提交表单
         */
        form.verify({ // TODO 自定以表单校验规则
            pass: function(value, item) {
                if (!/^[a-zA-Z]\w{5,9}$/.test(value)) {
                    return "以字母开头，长度在6~10之间，只能包含字母、数字和下划线";
                }
            },
            rePass: function (value, item) {
                var pass = $("input[name='pass']").val();
                if (value !== pass) {
                    return "两次密码输入不一致"
                }
            }
        });
        form.on('submit(update)', function (data) { // TODO 提交表单
            var pass = $.encryptRasLI(data.field.pass); // 按照公钥加密
            $.ajax({
                url: $.getProjectNameLI() + "/sys/admin/updateUserInfo",
                async: false,
                type: "post",
                dataType: "json",
                data: { id: [[${entity.id}]], pass: pass },
                success: function (res) {
                    layer.msg(res.code === 0 ? "更新成功" : res.message, {time: 1500},function () {
                        window.top.location.href = $.getProjectNameLI() + "/sys/admin/toLogin"; // 跳转到登陆页面
                    });
                }
                ,complete: function (xhr) {
                    $.ajaxCompleteLI(xhr);
                }
            });
            return false;
        });

    });
</script>

</html>